﻿using System;
using System.Diagnostics;
using System.Security;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using SfdClient.Messaging;
using SfdClient.Resources;

namespace SfdClient.ViewModel
{
    public class SettingsViewModel : WorkingViewModelBase
    {
        public SettingsViewModel()
        {
            if (!IsInDesignMode)
            {
                Messenger.Default.Register<NavigateToMessage>(this,
                    message=>
                        {
                            if (message.View != ApplicationView.SettingsView)
                                return;

                            IsStartingWithWindows = RegistryUtility.GetWindowsStartValue();

                            DefaultExcludedFilesPattern = Properties.Settings.Default.DefaultExcludedFilesPattern;
                            DefaultExcludedFoldersPattern = Properties.Settings.Default.DefaultExcludedFoldersPattern;
                            DefaultFileMaxSize = Properties.Settings.Default.DefaultFileMaxSize;
                        });
            }
        }

        #region IsStartingWithWindows Property

        /// <summary>
        /// The <see cref="IsStartingWithWindows" /> property's name.
        /// </summary>
        public const string IsStartingWithWindowsPropertyName = "IsStartingWithWindows";

        private bool _isStartingWithWindows;

        public bool IsStartingWithWindows
        {
            get { return _isStartingWithWindows; }

            set
            {
                if (_isStartingWithWindows == value) return;

                _isStartingWithWindows = value;
                RaisePropertyChanged(IsStartingWithWindowsPropertyName);
            }
        }

        #endregion

        #region DefaultExcludedFoldersPattern Property

        /// <summary>
        /// The <see cref="DefaultExcludedFoldersPattern" /> property's name.
        /// </summary>
        public const string DefaultExcludedFoldersPatternPropertyName = "DefaultExcludedFoldersPattern";

        private string _defaultExcludedFoldersPattern;

        public string DefaultExcludedFoldersPattern
        {
            get { return _defaultExcludedFoldersPattern; }

            set
            {
                if (_defaultExcludedFoldersPattern == value) return;

                _defaultExcludedFoldersPattern = value;
                RaisePropertyChanged(DefaultExcludedFoldersPatternPropertyName);
            }
        }

        #endregion

        #region DefaultExcludedFilesPattern Property

        /// <summary>
        /// The <see cref="DefaultExcludedFilesPattern" /> property's name.
        /// </summary>
        public const string DefaultExcludedFilesPatternPropertyName = "DefaultExcludedFilesPattern";

        private string _defaultExcludedFilesPattern;

        public string DefaultExcludedFilesPattern
        {
            get { return _defaultExcludedFilesPattern; }

            set
            {
                if (_defaultExcludedFilesPattern == value) return;

                _defaultExcludedFilesPattern = value;
                RaisePropertyChanged(DefaultExcludedFilesPatternPropertyName);
            }
        }

        #endregion

        #region DefaultFileMaxSize Property

        /// <summary>
        /// The <see cref="DefaultFileMaxSize" /> property's name.
        /// </summary>
        public const string DefaultFileMaxSizePropertyName = "DefaultFileMaxSize";

        private int _defaultFileMaxSize;

        public int DefaultFileMaxSize
        {
            get { return _defaultFileMaxSize; }

            set
            {
                if (_defaultFileMaxSize == value) return;

                _defaultFileMaxSize = value;
                RaisePropertyChanged(DefaultFileMaxSizePropertyName);
            }
        }

        #endregion  

        #region BackCommand

        private ICommand _backCommand;

        public ICommand BackCommand
        {
            get { return _backCommand ?? (_backCommand = new RelayCommand(ExecuteBackCommand, CanExecuteBackCommand)); }
        }

        public void ExecuteBackCommand()
        {
            if (!CanExecuteBackCommand())
                return;

            Messenger.Default.Send(new NavigateToMessage(ApplicationView.AzureProjectsView));
        }

        public bool CanExecuteBackCommand()
        {
            return !IsWorking;
        }

        #endregion

        #region SaveCommand

        private ICommand _saveCommand;

        public ICommand SaveCommand
        {
            get { return _saveCommand ?? (_saveCommand = new RelayCommand(ExecuteSaveCommand, CanExecuteSaveCommand)); }
        }

        public void ExecuteSaveCommand()
        {
            if (!CanExecuteSaveCommand())
                return;

            try
            {
                RegistryUtility.SetWindowsStartValue(IsStartingWithWindows);
            }
            catch (UnauthorizedAccessException)
            {
                Trace.WriteLine(ResourceLocalizer.Current["SettingsViewModel_PasAutoriseRegistre"], TraceLevel.Error.ToString());
            }
            catch(SecurityException)
            {
                Trace.WriteLine(ResourceLocalizer.Current["SettingsViewModel_ViolationAccessRegistre"], TraceLevel.Error.ToString());
            }
            catch(ArgumentException)
            {
                Trace.WriteLine(ResourceLocalizer.Current["SettingsViewModel_CleInconnue"], TraceLevel.Warning.ToString());
            }

            Properties.Settings.Default.DefaultExcludedFilesPattern = DefaultExcludedFilesPattern;
            Properties.Settings.Default.DefaultExcludedFoldersPattern = DefaultExcludedFoldersPattern;
            Properties.Settings.Default.DefaultFileMaxSize = DefaultFileMaxSize;

            Properties.Settings.Default.Save();
            
            ExecuteBackCommand();
        }

        public bool CanExecuteSaveCommand()
        {
            return !IsWorking;
        }

        #endregion
    }
}
